# coding=utf8
# $Id$

import urllib2, urllib
import re, os, errno, sys
from StringIO import StringIO
from lxml import etree

import socket
socket.setdefaulttimeout(35.0)

DOWNLOAD_DIR = '/Data/flash-crawl-data/4399/'

def get_by_id(id):
    url = 'http://www.4399.com/flash/%d.htm' % id

    print ''
    print url

    html = get_page(url);

    if not html:
        raise Exception('cannot get page', url)

    parser = etree.HTMLParser()
    tree   = etree.parse(StringIO(html), parser)

    try:
        r = tree.xpath('//table[@height="22"]/tr/td/a[3]/text()')
        name_cn = r[0].strip()
        # print name_cn
    except:
        raise Exception('cannot find name_cn', url)

    try:
        r = tree.xpath('//table[@height="22"]/tr/td/a[2]/text()')
        category = re.sub(u'小游戏', '', r[0]).strip()
        # print category
    except:
        raise Exception('cannot find category', url)

    # try:
    #     r = tree.xpath('//table[@height="22"]/tr/td/text()')
    #     file_size =re.sub(r'[^0-9]', '', r[2]).strip()
    #     # print file_size
    # except:
    #     raise Exception('cannot find category', url)
        
    # r = tree.xpath('//div[@id="mb_game"]/table[2]/tr/td/p[3]')
    # game_intro = r[0].strip()
    # print game_intro

    try:
        r = tree.xpath('//div[@id="mb_game"]/table[2]/tr/td/p//text()')
        game_help = '\n'.join([x for x in r if x.getparent().tag in ('p', 'font', 'br', 'div', 'span')])
    except:
        raise Exception('cannot find help', url)

    image = False
    try:
        r = tree.xpath('//div[@class="GamePic"]//img/@src')
        image = r[0]
    except IndexError:
        pass
    try:
        r = tree.xpath('//div[@class="GamePic2"]//img/@src')
        image = r[0]
    except IndexError:
        pass
    
    # print image
    if not image:
        raise Exception('cannot find image', url)

    try:
        r = tree.xpath('//td[@class="font14px"]/p/script')
        str100 = r[0].text
        flash = str100.split('"')[1]
        # print flash
    except: 
        raise Exception('cannot find flash', url)

    server_js = False
    r = tree.xpath('//head/script/@src')
    for ss in r:
        if ss[-4:] == '.gif':
           server_js = ss
           break

    if not server_js:
        raise Exception('cannot find server_js', url)

    server_js_url = 'http://www.4399.com' + server_js
    servers = get_page(server_js_url)
    if not servers:
        raise Exception('cannot get server_js', url)

    base_flash_url = servers.split('"')[1]
    if base_flash_url[:7] != 'http://':
        raise Exception('cannot parse server_js', servers)

    flash = base_flash_url + flash


    path = DOWNLOAD_DIR + str(id)[-2:] + '/'
    if len(path) == 1:
        path = '0' + path

    try:
        os.makedirs(path)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise e


    flash_file = path + str(id) + '.swf'
    # urllib.urlretrieve(flash, flash_file)

    if image:
        image_file = path + str(id) + '.jpg'
        urllib.urlretrieve(image, image_file)


    return {'id':id,
            'category':category, 
            'tag':'', 
            'name_cn':name_cn, 
            'game_help':game_help,
            'image':image,
            'flash':flash}




def get_page(url):
    """
    return HTML or False on error
    """

    html = False

    try:
        req = urllib2.Request(url=url)
        f = urllib2.urlopen(req)

        html = unicode(f.read(), 'gbk', 'ignore')
        # html = html.replace('charset=GB2312', 'charset=UTF-8')

        # html = f.read();
    except urllib2.HTTPError:
        pass

    return html


if __name__ == '__main__':

    id = 12345
    if len(sys.argv) > 1:
        id = int(sys.argv[1])

    print get_by_id(id)


